
;(function(win){
	var Pager = win.Pager = function(element){
		if(typeOf(element) === 'string'){
			element = document.getElement(element);
		}
		this.$el = element;
		this.pages = element.getElement('.pager-pages-wrap');
		this.preBtn = element.getElement('.pager-btn[role=pre]');
		this.nextBtn = element.getElement('.pager-btn[role=next]');
		this.ctnWidth = element.getElement('.pager-pages-container').getSize().x;
		this.curLeft = 0;
		this.step = 6;
		this.currentPage = 0;
		this.setup();
	}

	Pager.prototype.update = function(totalPages, currentPage){
		this.pages.empty();
		var currentPage = typeof currentPage === 'undefined' ? this.currentPage : currentPage;
		var pagesString = '';
		totalPages.times(function(key){
			key = key + 1;
			pagesString += '<li class="btn pager-page">' + key + '</li>';
		});
		this.pages.setStyle("width", totalPages*this.iw);
		this.pages.appendHTML(pagesString);
		this.pages.getElements('.pager-page')[currentPage].addClass('active');
		this.currentPage = currentPage;
		//console.log(this.currentPage);
		var currentLeft = this.curLeft;
		var ctnWidth = this.ctnWidth;
		var pagesWidth = this.pages.getSize().x;
		if(currentLeft + pagesWidth > ctnWidth && this.nextBtn.getStyle('display') === 'none'){
			this.nextBtn.setStyle('display', 'block');
		}
	}
	Pager.prototype.setup = function(){
		var self = this;
		var testPage = '<li class="btn pager-page">t</li>';
		this.pages.appendHTML(testPage);
		testPage = this.pages.getElement('.pager-page');
		var width = Number.from(testPage.getSize().x);
		var marginLeft = parseFloat(testPage.getStyle('margin-left'));
		var marginRight = parseFloat(testPage.getStyle('margin-right'));
		var individualWidth = width + marginLeft + marginRight;
		this.iw = individualWidth;
		this.pages.empty();
		this.pages.setStyle('width','0');
		this.nextBtn.addEvent('click', function(){
			var ctnWidth = self.ctnWidth;
			var pagesWidth = self.pages.getSize().x;
			var currentLeft = self.curLeft;
			var iw = self.iw;
			var step = self.step;
			var moveTo = iw * step;
			var asp = pagesWidth + currentLeft - ctnWidth;
			if(asp < 0){
				currentLeft = ctnWidth - pagesWidth;
				moveTo = 0;
			}else{
				moveTo = Math.min(moveTo, asp);
				currentLeft -= moveTo;
			}
			TweenLite.to(self.pages, 1, {left: String.from(currentLeft)});
			if(currentLeft + pagesWidth <= ctnWidth){
				self.nextBtn.setStyle('display', 'none');
			}
			if(currentLeft < 0 && self.preBtn.getStyle('display') === 'none'){
				self.preBtn.setStyle('display', 'block');
			}
			self.curLeft = currentLeft;

		});
		this.preBtn.addEvent('click', function(){
			var ctnWidth = self.ctnWidth;
			var pagesWidth = self.pages.getSize().x;
			var currentLeft = self.curLeft;
			var iw = self.iw;
			var step = self.step;
			var moveTo = iw * step;
			var asp = -currentLeft;
			if(asp < 0){
				currentLeft = 0;
				moveTo = 0;
			}else{
				moveTo = Math.min(moveTo, asp);
				currentLeft += moveTo;
			}
			TweenLite.to(self.pages, 1, {left: String.from(currentLeft)});
			if(currentLeft == 0){
				self.preBtn.setStyle('display', 'none');
			}
			if(currentLeft + pagesWidth - ctnWidth > 0 && self.nextBtn.getStyle('display') === 'none'){
				self.nextBtn.setStyle('display', 'block');
			}
			self.curLeft = currentLeft;
		});
	}
})(window)